﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StringDefs.Common ;

namespace StringDefs.NLP
{
    /// <summary>
    /// This namespace holds 
    /// </summary>
    public static class NLP
    {
        /// <summary>
        /// Returns Bigrams of the argument sentence or phrase. 
        /// </summary>
        /// <param name="sentence">The argument phrase for which we want to extract the bigrams.</param>
        /// <returns></returns>
        /// <remarks>This assumes a frequency of two. Thus the name Bigram</remarks>
        public static IList<string> Bigrams(this string sentence)
        {
            List<string> bigrams = new List<string>();
            string[] words = sentence.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < words.Length - 1; i++)
            {
                bigrams.Add(words[i] + " " + words[i + 1]);
            }
            return bigrams;
        }
        /// <summary>
        /// Returns collocations of a string
        /// </summary>
        /// <param name="sentence">the sentence or paragraph for which collocations need to be found </param>
        /// <returns></returns>
        /// <remarks>to know more about collocations visit http://www.englishclub.com/vocabulary/collocations.htm</remarks>
        public static IList<string> Collocations(this string sentence)
        {
            List<string> frequentBigrams = new List<string>();
            Dictionary<string, int> bigramHistogram = new Dictionary<string, int>();
            string[] words = sentence.Split(new char[] { ' '}, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < words.Length - 1; i++)
            {
                string bigram = words[i] + " " + words[i + 1];
                if(!bigramHistogram.ContainsKey (bigram ))
                    bigramHistogram.Add (bigram,1);
                else
                    bigramHistogram[bigram]++;

            }
            foreach (string k in bigramHistogram.Keys)
            {
                if (bigramHistogram[k] >= 2)
                    frequentBigrams.Add(k);
            }
            return frequentBigrams;
        }

    }
}
